package xcmg.device.web.sale;

import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xcmg.device.infra.AuthCode;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.sale.ReturnInService;
import xcmg.device.service.vo.delivery.ManualFinishVO;
import xcmg.device.service.vo.salesReturn.SalesReturnInAddVO;
import xcmg.device.service.vo.salesReturn.SalesReturnInVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;
import yb.ecp.fast.infra.util.ListUtil;

import java.util.Arrays;
import java.util.List;

/**
 * 退货申请
 * Created by Li Changjiang on 2018/3/6 0006.
 */
@RequestMapping("/returnIn")
@RestController
public class ReturnInController extends BasicController
{
    @Autowired
    private ReturnInService returnInService=null;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiOperation(value = "查询退货入库列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<SalesReturnInVO> condition) throws Exception
    {
        if (null == condition)
        {
            return actionResult(ErrorCode.IllegalArument);
        }

        if (null == condition.getFilters())
        {
            SalesReturnInVO filters = new SalesReturnInVO();
            filters.setOrgId(getOrgId());
            condition.setFilters(filters);
        }
        else
        {
            condition.getFilters().setOrgId(getOrgId());
        }

        PageCommonVO pageCommonVO = returnInService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }


    @RequestMapping(value = "/item", method = RequestMethod.GET)
    @ApiOperation(value = "查询退货入库单详情信息")
    @FastMappingInfo(needLogin = true)
    public ActionResult item(@RequestParam("id") String id)
    {
        if (StringUtils.isEmpty(id))
        {
            return actionResult(ErrorCode.IllegalArument);
        }
        return actionValueResult(returnInService.item(id));
    }

    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @ApiOperation(value = "删除退货入库单信息")
    @FastMappingInfo(needLogin = true,code = AuthCode.StockIn.RETURN_IN_DEL)
    public ActionResult remove(@RequestBody List<String> ids) throws Exception
    {
        if (ListUtil.isNullOrEmpty(ids))
        {
            return actionResult(ErrorCode.IllegalArument);
        }
        return actionResult(returnInService.remove(ids,getOrgId()));
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加退货入库单信息")
    @FastMappingInfo(needLogin = true,code = AuthCode.StockIn.RETURN_IN_ADD)
    public ActionResult add(@RequestBody SalesReturnInAddVO salesReturnInAddVO) throws Exception
    {
        if (null == salesReturnInAddVO)
        {
            return actionResult(ErrorCode.IllegalArument);
        }
        salesReturnInAddVO.setUserId(getUserId());
        salesReturnInAddVO.setUserName(getUserName());
        salesReturnInAddVO.setOrgId(getOrgId());

        return actionResult(returnInService.insert(salesReturnInAddVO,getDeptName()));
    }


    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "编辑退货入库单信息")
    @FastMappingInfo(needLogin = true,code = AuthCode.StockIn.RETURN_IN_EDIT)
    public ActionResult update(@RequestBody SalesReturnInAddVO salesReturnInAddVO) throws Exception
    {
        if (null == salesReturnInAddVO)
        {
            return actionResult(ErrorCode.IllegalArument);
        }
        salesReturnInAddVO.setUserId(getUserId());
        salesReturnInAddVO.setUserName(getUserName());
        salesReturnInAddVO.setOrgId(getOrgId());
        return actionResult(returnInService.update(salesReturnInAddVO));
    }

    @RequestMapping(value = "/exportAll", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "导出所有退货入库单")
    @FastMappingInfo(needLogin = true,code = AuthCode.StockIn.RETURN_IN_EXPORT)
    public void exportAll(SalesReturnInVO condition) throws Exception
    {
        if (null == condition)
        {
            condition = new SalesReturnInVO();
            condition.setOrgId(getOrgId());
        }
        else
        {
            condition.setOrgId(getOrgId());
        }
        returnInService.exportAll(condition);
    }

    @RequestMapping(value = "/export", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "导出所选退货入库单")
    @FastMappingInfo(needLogin = true,code = AuthCode.StockIn.RETURN_IN_EXPORT)
    public void export(String[] returnInIds) throws Exception
    {
        returnInService.export(Arrays.asList(returnInIds));
    }

    @RequestMapping(value = "/manualFinish", method = RequestMethod.POST)
    @ApiOperation("强制完成单据")
    public ActionResult manualFinish(@RequestBody ManualFinishVO manualFinishVO) throws Exception
    {
        return actionResult(returnInService.manualFinish(manualFinishVO.getBillNo(), manualFinishVO.getMaterialsNo(), getOrgId()));
    }

//    @RequestMapping(value = "/sapPost", method = RequestMethod.POST)
//    @ApiOperation(value = "退货入库过账")
//    @FastMappingInfo(needLogin = true,code = AuthCode.StockIn.RETURN_IN_POSTING)
//    public ActionResult sapPost(@RequestBody SalesReturnInVO salesReturnInVO) {
//        ErrorCode errorCode;
//        try {
//            errorCode = returnInService.sapPost(salesReturnInVO, getOrgId());
//        } catch (Exception e) {
//            LogHelper.monitor(e.getMessage());
//            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
//        }
//        return actionResult(errorCode);
//    }
//
//    @RequestMapping(value = "/sapRemove", method = RequestMethod.POST)
//    @ApiOperation(value = "退货入库过账删除")
//    @FastMappingInfo(needLogin = true,code = AuthCode.StockIn.RETURN_IN_POSTING_CANCEL)
//    public ActionResult sapRemove(@RequestBody SalesReturnInVO salesReturnInVO) {
//        ErrorCode errorCode;
//        try {
//            errorCode = returnInService.sapRemove(salesReturnInVO, getOrgId());
//        } catch (Exception e) {
//            LogHelper.monitor(e.getMessage());
//            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
//        }
//        return actionResult(errorCode);
//    }

}
